#!/bin/bash
# install.sh

if [ $UID != 0 ]
then
	echo must be run as root.
	exit 1
fi

USER=$(cat /etc/passwd | grep home | grep sh | cut -d: -f1)
if [ -e "/home/${USER}/Desktop" ]
then
	Desktop="Desktop"
else
	Desktop="桌面"
fi
DESKTOP="/home/${USER}/${Desktop}/"

INSTALL_DIR='/usr/local/share/rauth/'
EXEC_DIR='/usr/local/bin/'
NAME='rauth'
CWD=$(pwd)/

echo Installing...
echo coping files...
if ! [ -e ${INSTALL_DIR} ]
then
mkdir ${INSTALL_DIR}
fi

echo cp ${CWD}${NAME} ${EXEC_DIR}
cp ${CWD}${NAME} ${EXEC_DIR}

if [ -e $DESKTOP ]
then
	echo cp ${CWD}Rauth.desktop ${DESKTOP}
	cp ${CWD}Rauth.desktop ${DESKTOP}
fi

echo chown ${USER} ${DESKTOP}Rauth.desktop
chown ${USER} ${DESKTOP}Rauth.desktop
echo chown ${USER} ${EXEC_DIR}${NAME}
chown ${USER} ${EXEC_DIR}${NAME}


chmod +x ${EXEC_DIR}${NAME}

declare -a FILES
FILES="account.py const.py images rauth.py win.py auth.py icon.py __init__.py rauth scripts account.conf"
for file in $FILES
do
	echo "cp ${CWD}${file} ${INSTALL_DIR}${file} -r"
	cp ${CWD}${file} ${INSTALL_DIR}${file} -r
done

echo chown ${USER} ${INSTALL_DIR}images/rauth.png
chown ${USER} ${INSTALL_DIR}*

echo rauth have been installed successfully
